<template>
	<view>
		<j-cell-group>
			<template v-for="(item,index) in list">
				<j-cell :key="index" v-if="item.show" :title="item.title" @jftap="tapAction(item)" />
			</template>
		</j-cell-group>
		<j-modal :value="startDialog" :showCancel="false" :showConfirm="false">
			<view slot="title" class="flex align-center justify-between j-modal-title">
				<view style="width: 30%;" class=""></view>
				<view style="width: 30%;" class="hint">提示</view>
				<view style="width: 30%;text-align: end;" class="">
					<!-- <uni-icons @click="clearTimer" type="closeempty" size="25" /> -->
				</view>
			</view>
			<view slot="content" class="popupCont">
				<image class="bannerImg" src="https://statics.xmcsrv.net/weixin/operatingLock.png" mode="widthFix" />
				<view class="">请在门锁上按"8#"进入远程设置模式</view>
				<!-- <view class="countDown">{{start2countDown}}s</view> -->
			</view>
		</j-modal>
	</view>
</template>

<script>
	import {
		wakeUpLockBoardCodeMsg
	} from "@/utils/errorCode";
	const {
		getStatus,
		turnTenToSixteen
	} = require('@/utils/util.js');
	const {
		userManageList
	} = require('./userManage.js');
	const { handleStartErr } = require('@/jvss/pagesDevice/utils/index.js');
	export default {
		data() {
			return {
				device: {},
				list: [],
				userType: '',
				deviceStatus: '',

				startDialog: false,
				start2countDown: 30,
				startTimer: null,
			}
		},
		onLoad(opt) {
			this.userType = opt.userType
			let enData = decodeURIComponent(opt.device)
			this.device = JSON.parse(enData)
			let list = userManageList()
			let userList = this.userType.split('')
			this.list = list.map((item, index) => {
				return {
					...item,
					show: userList[index] == 1
				}
			})
			this.deviceStatus = getStatus(this.device.status)
			console.log('list===', this.list, userList, this.device, this.deviceStatus);
		},
		methods: {
			tapAction(item) {
				if (this.deviceStatus == 'deep' || this.deviceStatus == 'OffLine') {
					return uni.showToast({
						title: '设备已经处于深度休眠，无法远程唤醒',
						icon: 'none'
					})
				}
				// 如果处于浅休眠和在线状态
				this.checkWakeUp(item)
				uni.setStorageSync('startErrToPath','jvss/pagesDevice/device/device-live/door-lock/setting/setting-lexin')
				// uni.navigateTo({
				// 	// url:`${item.url}?userInfo=${JSON.stringify(item)}&device=${encodeURIComponent(JSON.stringify(this.device))}`
				// 	url:`./verifyPassword?type=1&userInfo=${JSON.stringify(item)}&device=${encodeURIComponent(JSON.stringify(this.device))}`
				// })
			},

			// 检测锁板唤醒状态
			checkWakeUp(item) {
				this.getDoorLockTransparent(item, 'Start')
			},
			// 唤醒锁板
			wakeUpStart2(item) {
				this.getDoorLockTransparent(item, 'Start2')
			},
			getDoorLockTransparent(item, status) {
				if (status != 'Start2') {
					uni.showLoading({
						mask: true
					})
				}
				let data = {
					Name: 'OPDoorLockProCmd',
					OPDoorLockProCmd: {
						Cmd: "SyncDoorStatus",
						DoorStatus: {
							Status: status
						},
						SessionID: 0x00000001
					}
				};
				let token = this.device.token;
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					uni.hideLoading()
					console.log('检测是否需要唤醒锁板', result);
					if (result.data.code == 2000) {
						let Err = turnTenToSixteen(result.data.data.DoorStatus.Err)
						if (result.data.data.DoorStatus.Err == 0) {
							if (status === 'Start') {
								// 指令发送成功后，改变缓存中start的状态为true
								uni.setStorage({
									key: 'StartFlag',
									data: {
										deviceNo: this.device.deviceNo,
										token: this.device.token,
										start: true
									},
									success: () => {
										console.log('Start指令发送成功')
										console.log(uni.getStorageSync('StartFlag'))
									}
								})
							} 
						}
						if (status === 'Start2') {
							this.clearTimer();
						}
						this.switchErr(Err, item, result)
					} else {
						uni.showToast({
							title: getApp().globalData.errCodeMsg.CodeMsg[result.data.code] || result.data
								.msg,
							icon: 'none',
							duration: 2000
						});
						setTimeout(() => {
							this.clearTimer();
						}, 1000)
					}
				}, false);
			},
			// 根据Err判断
			switchErr(Err, item, result) {
				switch (Err) {
					// 成功，跳转到具体设置页面
					case '0x00':
					case '0x12':
						this.jumpToSetting(item)
						break;
						// 需要唤醒，提示先唤醒锁板发Start2
					case '0x01':
						this.startDialog = true;
						// this.countDown()
						this.wakeUpStart2(item)
						break;
						// 	需要跳到管理员页面
					case '0x11':
						this.jumpToVerify(item)
						break;
					default:
					    this.clearTimer();
						uni.showToast({
							title: getApp().globalData.errCodeMsg.wakeUpLockBoardCodeMsg[Err] || result.data.msg,
							icon: 'none',
							duration: 2000
						});
				}
			},
			// 检测唤醒锁板倒计时
			countDown() {
				this.startTimer = setInterval(() => {
					this.start2countDown--;
					if (this.start2countDown == 0) {
						this.clearTimer()
					}
				}, 1000)
			},
			// 处理关闭倒计时
			clearTimer() {
				this.startDialog = false;
				// clearInterval(this.startTimer)
				// this.startTimer = null;
				// this.start2countDown = 30;
			},
			// 跳转到验证页面
			jumpToVerify(item) {
				uni.navigateTo({
					// url:`${item.url}?userInfo=${JSON.stringify(item)}&device=${encodeURIComponent(JSON.stringify(this.device))}`
					url: `./verifyPassword?type=1&userInfo=${JSON.stringify(item)}&device=${encodeURIComponent(JSON.stringify(this.device))}`
				})
			},
			// 成功后直接跳转到设置页面
			jumpToSetting(item) {
				let userUrl =
					`/jvss/pagesDevice/device/device-live/door-lock/userManage/userList?userInfo=${JSON.stringify(item)}&device=${encodeURIComponent(JSON.stringify(this.device))}`
				uni.navigateTo({
					url: userUrl
				})
			},
		}
	}
</script>

<style lang="scss">
	.popupCont {
		.countDown {
			font-size: 30rpx;
			font-weight: bold;
			color: #FF0025;
		}
	}

	.bannerImg {
		width: 80%;
		height: auto;
	}
</style>
